package cn.agiledata.bank.myspace.dao;

import java.util.List;

import cn.agiledata.bank.hibernateBean.PeerInnerAccount;
import com.vnv.infrastructure.logic.IPBHibernate3BaseMgr;

public class PeerInnerAccountDaoImpl extends IPBHibernate3BaseMgr implements PeerInnerAccountDao {
	/**
	 * 根据当前用户id,联系人email/手机得到联系人
	 * @param accountId
	 * @param peerInnerAccountId
	 * @return
	 */
	public PeerInnerAccount getPeerInnerAccount(Long accountId,String peerInnerAccountEmailOrMobile) {
		StringBuffer sb = new StringBuffer();
		sb.append(" from PeerInnerAccount p where 1=1 ");
        sb.append(" and p.account.id = ? ");
        sb.append(" and (lower(p.peerAccount.email) = ? or p.peerAccount.mobile = ?)");
        List list = getHibernateTemplate().find(sb.toString(),new Object[]{accountId,peerInnerAccountEmailOrMobile.toLowerCase(),peerInnerAccountEmailOrMobile});
        if(list == null) { 
        	return null;
        } else {
        	if(list.size() == 0) {
        		return null;
        	} else {
        		return (PeerInnerAccount)list.get(0);
        	}
        }
	}
	/**
	 * 根据id得到联系人
	 * @param id
	 * @param peerInnerAccountId
	 * @return
	 */
	public PeerInnerAccount getPeerInnerAccount(long id) {
		return (PeerInnerAccount)getHibernateTemplate().get(PeerInnerAccount.class, new Long(id));
	}

	/**
	 * 保存或者更新关联账户
	 * @param p
	 */
	public void saveOrUpdatePeerInnerAccount(PeerInnerAccount p) {
		getHibernateTemplate().saveOrUpdate(p);
	}
	/**
	 * 删除关联账户
	 * @param p
	 */
	public void deletePeerInnerAccount(PeerInnerAccount p) {
		getHibernateTemplate().delete(p);
	}
}
